package org.adream.account.service;

import java.util.List;

import org.adream.account.dao.LoginDao;
import org.adream.account.dao.SchoolBasicInfoDao;
import org.adream.account.dao.UserDao;
import org.adream.account.dao.UserRoleLinkDao;
import org.adream.account.entity.Dream2020SchoolEntity;
import org.adream.account.entity.SchoolBasicInfoEntity;
import org.adream.account.entity.UserEntity;
import org.adream.account.model.LoginModel;
import org.adream.account.model.ResultModel;
import org.adream.account.util.ChineseCharToEnUtil;
import org.adream.account.util.Constant;
import org.adream.account.util.IdGenerator;
import org.adream.account.util.PasswordUtil;
import org.adream.account.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.druid.util.StringUtils;

@Service
public class SchoolBasicInfoService {
	
	private static final Logger logger = LoggerFactory.getLogger(SchoolBasicInfoService.class);
	
	@Autowired
	private SchoolBasicInfoDao schoolBasicInfoDao;
	
	@Autowired
	private UserDao userDao;
	
	@Autowired
	private UserRoleLinkDao userRoleDao;
	
	@Autowired
	private LoginDao loginDao;
	
	@Autowired
	private Dream2020SchoolService dream2020SchoolService;
	
	/**
	 * 同步学校基本信息
	 * @return
	 * @throws ADreamServiceException
	 */
	public ResultModel<String> synSchoolBasicInfo() throws ADreamServiceException {
		List<SchoolBasicInfoEntity> schoolBasicInfos = schoolBasicInfoDao.querySchoolBasicInfos(Constant.ONE);
		int schoolBasicInfosSize = schoolBasicInfos.size();
		for (int i = 0; i < schoolBasicInfosSize; i++) {
			SchoolBasicInfoEntity schoolBasicInfo = schoolBasicInfos.get(i);
			String sname = schoolBasicInfo.getSname();
			String email = schoolBasicInfo.getEmail();
			int province = schoolBasicInfo.getProvince();
			int city = schoolBasicInfo.getCity();
			int area = schoolBasicInfo.getArea();
			Dream2020SchoolEntity dream2020SchoolEntity = new Dream2020SchoolEntity();
			dream2020SchoolEntity.setName(sname);
			//设置首字母
			dream2020SchoolEntity.setFirstLetter(ChineseCharToEnUtil.getFirstLetter(sname.substring(0,1)).toUpperCase());
			dream2020SchoolEntity.setSponsors(schoolBasicInfo.getSponsors() == null ? "" : schoolBasicInfo.getSponsors());
			dream2020SchoolEntity.setRemark(schoolBasicInfo.getRemark() == null ? "" : schoolBasicInfo.getRemark());
			dream2020SchoolEntity.setSchoolNumber(schoolBasicInfo.getAsid());
			dream2020SchoolEntity.setEmail(email);
			dream2020SchoolEntity.setPhone(schoolBasicInfo.getPhone() == null ? "" : schoolBasicInfo.getPhone());
			dream2020SchoolEntity.setZipCode(schoolBasicInfo.getZipCode() == null ? "" : schoolBasicInfo.getZipCode());
			dream2020SchoolEntity.setProvince(province);
			dream2020SchoolEntity.setCity(city);
			dream2020SchoolEntity.setArea(area);
			dream2020SchoolEntity.setLocation(schoolBasicInfo.getLocation() == null ? "" : schoolBasicInfo.getLocation());
			dream2020SchoolEntity.setApplytime(schoolBasicInfo.getApplytime());
			dream2020SchoolEntity.setTeacherCnt(schoolBasicInfo.getTeacherCnt());
			dream2020SchoolEntity.setStudentCnt(schoolBasicInfo.getStudentCnt());
			dream2020SchoolEntity.setIsDel(schoolBasicInfo.getDr() == Constant.ZERO ? 1 : 0);
			
			//判断学校是否删除&uid是否为空
			if(schoolBasicInfo.getDr() == Constant.ONE && StringUtils.isEmpty(schoolBasicInfo.getUid())) {
				//根据email查询是否有对应的用户
				UserEntity user = userDao.queryUserByEmail(schoolBasicInfo.getEmail());
				if(user == null) {
					//创建用户
					user = new UserEntity();
					String uid = IdGenerator.getGuid();
					user.setUid(uid);
					user.setUname(sname);
					user.setEmail(email);
					user.setCreator("system");
					user.setProvince(province);
					user.setCity(city);
					user.setArea(area);
					user.setDr(Constant.ONE);
					userDao.addUser(user);
					//添加学校账号 邮箱登录信息
					LoginModel login = new LoginModel();
					login.setUid(uid);
					String salt = Utils.getSixUuid();
					login.setSalt(salt);
					//密码可能需要重新生成
					login.setPassword(PasswordUtil.generate(schoolBasicInfo.getAsid(), salt));
					login.setLogin(schoolBasicInfo.getEmail());
					login.setMethod(Constant.APWD);
					loginDao.replaceLogin(login); 
				}
				schoolBasicInfo.setUid(user.getUid());
				//更新学校基本信息uid
				schoolBasicInfoDao.updateSchoolUid(schoolBasicInfo);
			}
			//判断是否包含梦想中心学校角色
			if(schoolBasicInfo.getSrId().contains(Constant.ADREAM_CENTER_SCHOOL_ROLE_ID)) {
				//添加学校账号 梦想中心角色
				userRoleDao.replaceUserRoleById(schoolBasicInfo.getUid(), Constant.DREAM_SCHOOL_ROLE_ID);
				dream2020SchoolEntity.setAdreamCenter(true);
			}
			//同步至盒子
			dream2020SchoolService.addOrUpdateDream2020School(dream2020SchoolEntity);
		}
		return new ResultModel<String>(ResultModel.SUCCESS, "同步学校成功", null);
	}
}